package com.wavehi.exce;

import com.wavehi.pojo.Person;
import org.junit.Test;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

/**
 * @Author WaveHi
 * @Date 25/8/2021 下午 4:47
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description Test the use of java.beans package.
 */
public class IntrospectionDemo {

    @Test
    public void testIntrospector() throws IntrospectionException {

        //获取BeanInfo对象
        BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);

        //获取属性描述符
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

        //获取属性名
        PropertyDescriptor propertyDescriptor = propertyDescriptors[0];

        //获取属性的getter方法
        Method readMethod = propertyDescriptor.getReadMethod();

        //获取属性的setter方法
        Method writeMethod = propertyDescriptor.getWriteMethod();

    }

    @Test
    public void testIntrospectorUp() throws IntrospectionException {

        //获取内省信息
        BeanInfo beanInfo = Introspector.getBeanInfo(Person.class, Introspector.IGNORE_ALL_BEANINFO);

        //获取属性描述符
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

        //获取属性名
        for(PropertyDescriptor propertyDescriptor : propertyDescriptors) {

            //输出属性名
            System.out.println("The name of property is :" + propertyDescriptor.getName());

        }

    }

}
